home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Transactor
/
Transactor_23_1988_Transactor_Publishing.d64
/
brk trap.cbm
< prev
next >
Wrap
Text File
|
2023-02-26
|
3KB
|
157 lines
.opt nosym
;put"@:s/brk"
;********************************
;* *
;* simulating a trap with brk *
;* -------------------------- *
;* *
;* *
;* brk vector is diverted so *
;* that "invisible" subroutines *
;* can be called. *
;* *
;* *
;* - by tom hughes v022287 - *
;* *
;********************************
.skip
;
;c64 equates
;
cbinv = $0316 ;brk vector (2)
chrout = $ffd2 ;output a char
clrchn = $ffcc ;i/o to defaults
getin = $ffe4 ;input a char
memory = $8d ;temp storage (2)
oldbrk = $8b ;storage for standard brk (2)
stack = $0100 ;65xx stack location
tidyup = $febc ;recover from interrupt
.skip
* = $8000 ;sys 32768
.skip
;------------------------------
;set brk vector to our routine
;------------------------------
.skip
;in actual use, this would be a subroutine
;called once to divert the brk vector.
.skip
jsr clrchn
sei ;disable interrupts
ldx cbinv
ldy cbinv+1
stx oldbrk ;save the old brk vector
sty oldbrk+1
ldx #<newbrk ;then set new vector
ldy #>newbrk
stx cbinv
sty cbinv+1
cli ;enable interrupts
.page
;------------------------------
;demo brk handler
;------------------------------
.skip
;this is just an example of how you
;would use brk from within a program.
.skip
demo5 ldy #0
demo10 lda prompt,y ;print "number?"
beq demo20
jsr chrout
iny
bne demo10
demo20 jsr getin ;check the keyboard
cmp #3 ;(if stop key, quit)
beq quit
cmp #'1 ;for numbers 1 thru 3
bcc demo20
cmp #'4
bcs demo20
jsr chrout
and #%00001111 ;make # hex 1 - 3
sta trpnm ;save in our own program
lda #13 ;print a carriage return
jsr chrout
brk ;execute trap
trpnm .byt 0 ;(trap #)
jmp demo5 ;after brk, prg continues here
.skip
quit sei
ldx oldbrk
ldy oldbrk+1
stx cbinv
sty cbinv+1
cli
rts ;back to basic
.skip
prompt .byt 13,13,'number (1 - 3)? ',0
.page
;==============================
;new brk routine
;==============================
.skip
;entry: (1) interrupts disabled (except nmi)
; so jiffy clock is off.
;
; (2) on entry stack looks like this...
; (assuming old sp was at $f6)
;
; $01f6 <- old sp
; $01f5 pch (stack+6)
; $01f4 pcl (stack+5)
; $01f3 sr (stack+4)
; $01f2 .a (stack+3)
; $01f1 .x (stack+2)
; $01f0 .y (stack+1)
; $01ef <- current sp
;
; (3) expects trap # after brk instr.
; (this location can be found by
; using the pc saved on stack -1.)
;
newbrk tsx ;get current sp to .x
lda stack+6,x ;to find pc-high
sta memory+1
lda stack+5,x ;and pc-low on the stack
sta memory ;save this address
bne new10 ;and subtract -1 from it
dec memory+1 ;so we can locate trap #
new10 dec memory
ldy #0
lda (memory),y ;get trap #
tay ;adjust it so 1-3
dey ;is now 0-2
tya
asl a ;multiply this # by 2
tay
lda table,y ;and use it to look up
sta memory ;trap addresses
iny
lda table,y
sta memory+1
jmp (memory) ;go to a trap routine
.skip
;trap addresses
;
table .word trap1
.word trap2
.word trap3
.page
;//////////////////////////////
;demo trap routines
;//////////////////////////////
.skip
trap1 lda #'1
jsr chrout
jmp tidyup ;must end with this
.skip
trap2 lda #'2
jsr chrout
jmp tidyup
.skip
trap3 lda #'3
jsr chrout
jmp tidyup
.end